import Vue from 'vue'
import Router from 'vue-router'
import {getStore} from "../config/mUtils";


Vue.use(Router)

const vm=new Vue();

const Login = r => require.ensure([], () => r(require('../page/Login')), 'Login');
const Manage = r => require.ensure([], () => r(require('../page/Manage')), 'Manage');
const Home = r => require.ensure([], () => r(require('../page/Home')), 'Home');
const Money = r => require.ensure([], () => r(require('../page/Money')), 'Money');
const  Financial= r => require.ensure([], () => r(require('../page/Financial')), 'Financial');
const  Cash= r => require.ensure([], () => r(require('../page/Cash')), 'Cash');
const  Goods= r => require.ensure([], () => r(require('../page/Goods')), 'Goods');
const  EditGoods= r => require.ensure([], () => r(require('../page/EditGoods')), 'EditGoods');
const  PublishGoods= r => require.ensure([], () => r(require('../page/PublishGoods')), 'PublishGoods');
const  OrderManage= r => require.ensure([], () => r(require('../page/OrderManage')), 'OrderManage');
const  AgentManage= r => require.ensure([], () => r(require('../page/AgentManage')), 'AgentManage');
const  AddAgent= r => require.ensure([], () => r(require('../page/AddAgent')), 'AddAgent');
const  EditAgent= r => require.ensure([], () => r(require('../page/EditAgent')), 'EditAgent');
const  PublishNews= r => require.ensure([], () => r(require('../page/PublishNews')), 'PublishNews');
const  SetUp= r => require.ensure([], () => r(require('../page/SetUp')), 'SetUp');
const  SetUpPermission= r => require.ensure([], () => r(require('../page/SetUpPermission')), 'SetUpPermission');
const  EditFinal= r => require.ensure([], () => r(require('../page/EditFinal')), 'EditFinal');
const  FinalCount= r => require.ensure([], () => r(require('../page/FinalCount')), 'FinalCount');
const  OrderCount= r => require.ensure([], () => r(require('../page/OrderCount')), 'OrderCount');
const  PayTypeCount= r => require.ensure([], () => r(require('../page/PayTypeCount')), 'PayTypeCount');
const  RangeApply= r => require.ensure([], () => r(require('../page/RangeApply')), 'RangeApply');
const  StoreApply= r => require.ensure([], () => r(require('../page/StoreApply')), 'StoreApply');
const  NewsList= r => require.ensure([], () => r(require('../page/NewsList')), 'NewsList');
const router=new Router({
  routes: [
    {
      path: '/',
      component: Login
    },
    {
      path: '/manage',
      component: Manage,
      children: [{
        path: '',
        component: Home,
        meta: [],
      },
        {
        path: '/money',
          name:'Money',
        component: Money,
        meta: ['账户资金'],
      },
        {
          path: '/fd',
          name:'Financial',
          component: Financial,
          meta: ['财务报表'],
        },
        {
          path: '/edit_final',
          name:'EditFinal',
          component: EditFinal,
          meta: ['财务报表','编辑代理商'],
        },
        {
          path: '/final_count',
          name:'FinalCount',
          component: FinalCount,
          meta: ['财务报表','财务统计'],
        },
        {
          path: '/order_count',
          name:'OrderCount',
          component: OrderCount,
          meta: ['财务报表','订单统计'],
        },
        {
          path: '/pay_type_count',
          name:'PayTypeCount',
          component: PayTypeCount,
          meta: ['财务报表','支付统计'],
        },
        {
          path: '/cash',
          name:'Cash',
          component: Cash,
          meta: ['申请提现'],
        },
        {
          path: '/goods',
          name:'Goods',
          component: Goods,
          meta: ['商品管理'],
        },
        {
          path: '/edit_goods',
          name:'EditGoods',
          component: EditGoods,
          meta: ['商品管理','编辑商品'],
        },
        {
          path: '/publish_goods',
          name:'PublishGoods',
          component: PublishGoods,
          meta: ['商品管理','发布商品'],
        },
        {
          path: '/order',
          name:'OrderManage',
          component: OrderManage,
          meta: ['订单管理'],
        },
        {
          path: '/agent',
          name:'AgentManage',
          component: AgentManage,
          meta: ['代理商管理'],
        },
        {
          path: '/add_agent',
          name:'AddAgent',
          component: AddAgent,
          meta: ['代理商管理','添加代理商'],
        },
        {
          path: '/edit_agent',
          name:'EditAgent',
          component: EditAgent,
          meta: ['代理商管理','编辑代理商'],
        },
        {
          path: '/range_apply',
          name:'RangeApply',
          component: RangeApply,
          meta: ['区域代理申请'],
        },
        {
          path: '/store_apply',
          name:'StoreApply',
          component: StoreApply,
          meta: ['门店申请管理'],
        },
        {
          path: '/news',
          name:'PublishNews',
          component: PublishNews,
          meta: ['消息发布'],
        },
        {
          path: '/news_list',
          name:'NewsList',
          component: NewsList,
          meta: ['消息发布','消息列表'],
        },
        {
          path: '/set_up',
          name:'SetUp',
          component: SetUp,
          meta: ['设置'],
        },
        {
          path: '/set_up_permissions',
          name:'SetUpPermission',
          component: SetUpPermission,
          meta: ['权限设置'],
        },
      ]
    },
  ]
})

const routeData=[
  ['/manage'],
  ['/money'],
  ['/fd','/final_count','/order_count','/pay_type_count','/edit_final'],
  ['/cash'],
  ['/goods','/edit_goods','/publish_goods'],
  ['/order'],
  ['/agent','/edit_agent','/add_agent'],
  ['/news','/news_list'],
  ['/set_up'],
  ['/set_up_permissions'],
  ['/range_apply'],
  ['/store_apply'],
]
/**
 *
 * @param f_id 用户的function_id
 * @returns {Array} 用户的所有路由权限路径数组
 */
const userCtrl=(f_id)=>{
  let curArr=[]          //新建一个数组用来放，f_id的路由权限
  routeData.map((item,index)=>{
    if(f_id-1==index){
      curArr.push(...item)
    }
  })
  return curArr
}
/**
 *
 * @param path 要去的路由
 * @returns {boolean} 是否有权限去
 */
const isHaveRoute=(path)=>{
  let routeArr=[]
  let ADF=JSON.parse(getStore('ADF'))  //用户的控制权限信息
  for(let item of ADF){
    routeArr.push(...userCtrl(item.function_id))
  }
  // console.log('routeArr'+routeArr)
   let isHave=routeArr.includes(path)
  return isHave
}
/**
 * 在路由跳转之前做权限控制
 */
router.beforeEach((to, from, next) => {
  if(to.path=='/'){
    next()
    return
  }
  let volid=isHaveRoute(to.path)   //是否有要去(to.path)的路由权限
   if(!volid){
     vm.$message({
       type: 'error',
       message: '你还没有没有权限！'
     });
   }else {
     next();
   }
})
/**
 * 当用户注销之后重新刷新页面，
 * 再次进入页面会重新走vue的生命周期
 */
router.afterEach((to, from) => {
  if(to.path=='/'&&from.path!='/'){
    window.location.reload();
  }
})
export default router
